package com.gdut.simple.presenter;

import com.gdut.simple.common.RequestCallback;
import com.gdut.simple.contract.PostDetailContract;
import com.gdut.simple.data.entity.Comment;
import com.gdut.simple.model.IPostDetailModel;
import com.gdut.simple.model.impl.PostDetailModelImpl;

import java.util.List;

public class PostDetailPresenter implements PostDetailContract.Presenter {

    private PostDetailContract.View mPostDetailView;
    private IPostDetailModel mPostDetailModel;

    public PostDetailPresenter(PostDetailContract.View mPostDetailView) {
        this.mPostDetailView = mPostDetailView;
        mPostDetailModel = new PostDetailModelImpl();
        this.mPostDetailView.setPresenter(this);
    }

    @Override
    public void getCommentList() {
        mPostDetailModel.getCommentList(mPostDetailView.getPost(), new RequestCallback<List<Comment>>() {
            @Override
            public void onSuccess(List<Comment> comments) {
                mPostDetailView.getItemList().clear();
                mPostDetailView.getItemList().addAll(comments);
                mPostDetailView.refreshList();
            }

            @Override
            public void onFailure(String message) {
                mPostDetailView.showMessage(message);
            }
        });
    }

    @Override
    public void comment() {
        mPostDetailView.showProgress("发送中...");
        mPostDetailModel.comment(mPostDetailView.getComment(), mPostDetailView.getUser(), mPostDetailView.getPost(), new RequestCallback<Comment>() {
            @Override
            public void onSuccess(Comment comment) {
                mPostDetailView.hideProgress();
                mPostDetailView.getItemList().add(comment);
                mPostDetailView.refreshList();
            }

            @Override
            public void onFailure(String message) {
                mPostDetailView.hideProgress();
                mPostDetailView.showMessage(message);
            }
        });
    }
}
